package com.just521.xinshoutong.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.just521.xinshoutong.R;
import com.just521.xinshoutong.json.ToDo;
import com.just521.xinshoutong.json.ToDoItem;
import com.just521.xinshoutong.view.ToDoEditView.OnKeyClickListner;

public class ToDoView extends LinearLayout
implements NoteElePlugin{
	ListView mlistview;
	Animation animationin;
	Animation animationout;
	String mpos;
	TextView mcurText;
	ToDoEditView mcurEditText;
	List<ToDoItem> mitems;
	String mtitle;
	
	List<ToDoItem> mitems_init;
	String mtitle_init;
	
	boolean mishavetitle;
	public ToDoView(Context context) {
		super(context);
		init(context,null);
	}
	
	public ToDoView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}
	private void init(Context context, AttributeSet attrs){
		setOrientation(LinearLayout.VERTICAL);
		setBackgroundResource(R.drawable.text_field_bg);
		setPadding(10, 10, 10, 10);
		
	}
	public void orignalData(Object data){
		if(data!=null){
			ToDo todo=(ToDo)data;
			mtitle_init=todo.getTitle();
			mitems_init=todo.getItems();
		}
	}
	public void initData(Object data){
		if(data==null){
			if(mishavetitle){
				
				insertItem(null,true);
			}else{
				insertTitle(null);
			}
		}else{
			// insert items view
			ToDo todo=(ToDo)data;
			mitems=(List<ToDoItem>)todo.getItems();
			mtitle=todo.getTitle();
			insertTitle(mtitle);
			if(mitems!=null){
				
				for(ToDoItem item:mitems){
					
					insertItem(item.getContent(),false);
				}
			}
			insertItem(null,false);
		}
		//insert edit view
		
	}
	public Object getData(){
		ToDo data=new ToDo();
		data.setTitle(mtitle);
		data.setItems(mitems);
		return data;
	}
	public void hideSoftInput(){
		
		postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				((InputMethodManager)getContext().getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(mcurEditText.getWindowToken(), 0);
				
			}
		}, 300);
		postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				mcurEditText.clearFocus();
			}
		}, 500);
	}
	void insertTitle(String text){
		LayoutInflater inf= LayoutInflater.from(getContext());
		View layout= inf.inflate(R.layout.todo_title, null);
		mcurEditText=(ToDoEditView)layout.findViewById(R.id.todo_title_text_et);
		mcurText=(TextView)layout.findViewById(R.id.todo_title_text_tv);
		mcurEditText.setOnKeyClickListner(new OnKeyClickListner() {
			
			@Override
			public void onEnterClick() {
				// TODO Auto-generated method stub
				//save cur item
				mtitle=mcurEditText.getText().toString();
//				mcurText.setText(mtitle);
//				mcurText.setVisibility(View.VISIBLE);
//				mcurEditText.setVisibility(View.GONE);
				insertItem(null,true);
			}
		});
		if(text==null){
			
			this.postDelayed(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					mcurEditText.requestFocus();
					mcurEditText.showKeyboard();
				}
			}, 500);
		}else{
			mcurText.setText(text);
			mcurText.setVisibility(View.VISIBLE);
			mcurEditText.setVisibility(View.GONE);
		}
		addView(layout);
		mishavetitle=true;
	}
	private void insertItem(String text,boolean bShowKeyboard){
		if(mcurText!=null && mcurText.getVisibility()!=View.VISIBLE){
			mcurEditText.setVisibility(View.GONE);
			mcurText.setVisibility(View.VISIBLE);
			mcurText.setText(mcurEditText.getText());
		}
		LayoutInflater inf= LayoutInflater.from(getContext());
		View layout= inf.inflate(R.layout.todoitem, null);
		mcurEditText=(ToDoEditView)layout.findViewById(R.id.todoitem_text_et);
		mcurEditText.setOnKeyClickListner(new OnKeyClickListner() {
			
			@Override
			public void onEnterClick() {
				// TODO Auto-generated method stub
				//save cur item
				ToDoItem temp= new ToDoItem();
				temp.setContent(mcurEditText.getText().toString());
				temp.setTodoid(getMaxID());
				if(mitems==null){
					mitems=new ArrayList<ToDoItem>();
				}
				mitems.add(temp);
				insertItem(null,true);
			}
		});
		mcurText=(TextView)layout.findViewById(R.id.todoitem_text_tv);
		if(text==null){
			if(bShowKeyboard){
				
				this.postDelayed(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						mcurEditText.requestFocus();
						mcurEditText.showKeyboard();
					}
				}, 500);
			}else{
			}
		}else{
			mcurText.setText(text);
			mcurText.setVisibility(View.VISIBLE);
			mcurEditText.setVisibility(View.GONE);
		}
		
		this.addView(layout);
	}
	long getMaxID(){
		long maxid=0;
		if(mitems!=null){
			
			for(ToDoItem rec:mitems){
				if(rec.getTodoid()>maxid){
					maxid=rec.getTodoid();
				}
			}
		}
		return maxid+1;
	}
	public void showEditView(){
		
	}
	public void onLongClick(){
		
	}
	public String getViewType(){
		return "todo";
	}
	public boolean isDataChanged(){
		boolean b=false;
		do{
			if(mtitle!=null && !mtitle.equals(mtitle_init)){
				b=true;
				break;
			}
			if(mitems==null || mitems.size()==0){
				break;
			}
			for(ToDoItem item:mitems){
				boolean bisfound=false;
				for(ToDoItem item_init:mitems_init){
					if(item.getTodoid()==item_init.getTodoid()){
						bisfound=true;
						if(item.getContent()!=null && !item.getContent().equals(item_init.getContent())){
							b=true;
							break;
						}
					}
				}
				if(b){
					break;
				}
				if(!bisfound){
					b=true;
					break;
				}
			}
			
		}while(false);
		return b;
	}
	public void setOnItemClickListener (AdapterView.OnItemClickListener listener){
		mlistview.setOnItemClickListener(listener);
	}
	public void setListData(String[] list){
		List<HashMap<String,String>> inlist=new ArrayList<HashMap<String,String>>();
		for(String text:list){
			HashMap<String,String> item=new HashMap<String,String>();
			item.put("text", text);
			inlist.add(item);
		}
		SimpleAdapter adapter=null;
		if(mpos.equals("middle")){
			adapter=new SimpleAdapter(getContext(), inlist, R.layout.simple_list_item_1_whilte, new String[]{"text"}, new int[]{android.R.id.text1});
		}else if(mpos.equals("right")){
			adapter=new SimpleAdapter(getContext(), inlist, R.layout.simple_list_item_1_black, new String[]{"text"}, new int[]{android.R.id.text1});
		}
		mlistview.setAdapter(adapter);
	}
	public void processHit(MotionEvent event){
		Rect rect=new Rect();
		
		getHitRect(rect);
		if((event.getX()<rect.left || event.getX()>rect.right) || (event.getY()<rect.top || event.getY()>rect.bottom)){
			hideanimation();
		}
	}
	public void showanimation(){
		setVisibility(View.VISIBLE);
		if(animationout==null){
			
			animationout = AnimationUtils.loadAnimation(getContext(), R.anim.toast_func_out);
		}
		this.startAnimation(animationout);
	}
	public void hideanimation(){
		if(animationin==null){
			
			animationin = AnimationUtils.loadAnimation(getContext(), R.anim.toast_func_in);
		}
		if(getVisibility()!=View.GONE){
			
			this.startAnimation(animationin);
			setVisibility(View.GONE);
		}
	}
}
